package com.linjiahao.userservice.controller;


import com.linjiahao.commonutils.JwtUtils;
import com.linjiahao.commonutils.ResultMap;
import com.linjiahao.userservice.entity.Friends;
import com.linjiahao.userservice.entity.UcenterMember;
import com.linjiahao.userservice.service.FriendsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-10-27
 */
@RestController
@RequestMapping("/userservice/friends")
public class FriendsController {

    @Autowired
    FriendsService friendsService;

    @GetMapping("addFriend/{her_id}")
    public ResultMap addFriendByMe(@PathVariable String her_id, HttpServletRequest request){
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        Friends friends = new Friends();
        friends.setId(memberId);
        friends.setFriendId(her_id);
        boolean save = friendsService.saveOne(friends);
        Friends friends2 = new Friends();
        friends2.setId(her_id);
        friends2.setFriendId(memberId);
        boolean one = friendsService.saveOne(friends2);
        if(save && one){
            return ResultMap.ok();
        }
        return ResultMap.error();
    }

    @GetMapping("getFriendList")
    public ResultMap getFriends(HttpServletRequest request){
        List<UcenterMember> freindList = friendsService.getFreindList(JwtUtils.getMemberIdByJwtToken(request));
        return ResultMap.ok().data("friends",freindList);
    }

}

